草庐IT

C++ 11:std::thread池化?

全部标签

c++ - 使用 C++11 迭代语法时从 STL 列表中删除

这个问题在这里已经有了答案:关闭10年前。PossibleDuplicate:Erasinganelementfromacontainerwhileinsidearange-basedforloopSimilartothis,您可以在使用新的for(autoitem:list)语法从STL列表中删除它吗?这是一个完整的例子(崩溃了!)#includeusingnamespacestd;intmain(){listli;li.push_back(4);li.push_back(5);li.push_back(6);for(autonum:li){if(num==5)li.remove(n

c++ - C++11 theads 的最基本并行化失败

我尝试通过g++4.7使用C++11theading库。首先我有一个问题:是否预计下一个版本不需要手动链接pthread库?所以我的程序是:#include#include#includevoidf(inti){std::coutt;for(inti=0;i我编译:g++-4.7-Wall-Wextra-Winline-std=c++0x-pthread-O3helloworld.cpp-ohelloworld它返回:Helloworldfrom:Helloworldfrom:Helloworldfrom:322purevirtualmethodcalledterminatecalle

c++ - 编译器如何传递 `std::initializer_list` 值? (或 : how can I get around a universal overload with one? )

Continuingmysaga,我意识到我可以使用单个std::initializer_list参数来重载我的访问函数:classarray_md{//...my_type&operator[](size_typei){/*Lotsofcode*/}my_typeconst&operator[](size_typei)const{/*sameLotsofcode,with"const"sprinkledin*/}my_type&operator[](std::initializer_listi){/*Lotsofdifferentcode*/}my_typeconst&operato

c++ - 是否有任何静态分析工具可以检查 Rule of 3(或 Rule of 5 C++11)

关闭。这个问题不符合StackOverflowguidelines.它目前不接受答案。我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。关闭8个月前。Improvethisquestion我目前正在开发一个建立在沙子基础上的代码库。据称经过测试的库中有许多类违反了“3法则”。大多数声明了一个非平凡的析构函数,但缺少复制构造函数或赋值运算符。是否有任何编译器标志(gcc)或静态分析工具会在类违反规则3时发出警告?目前我们将Coverity与GCC4.4版结合使用。

c++ - 我们应该在何时、何地以及为什么使用 "BigObject&& rv = std::move(big_obj);"?

我的编译器是最新的VC++2013预览版。#includestructBigObject{...};voidf(BigObject&&){}voidf(BigObject&){}voidf(BigObject){}intmain(){BigObjectbig_obj;BigObject&r1=big_obj;//OK.BigObject&&r2=big_obj;//errorC2440BigObject&&r3=std::move(big_obj);//OK.BigObject&&r4=r3;//errorC2440f(r3);//errorC2668:'f':ambiguouscal

c++ - to_string 不是 std 的成员,g++ (mingw)

我正在制作一个小型词汇内存程序,其中会随机闪现单词以了解含义。正如BjarneStroustroup告诉我们的那样,我想使用标准C++库,但我一开始就遇到了一个看似奇怪的问题。我想将一个long整数更改为std::string以便能够将其存储在文件中。为此,我使用了to_string()。问题是,当我用g++(版本4.7.0,如其--‍version标志中所述)编译它时,它说:PSC:\Users\Anurag\SkyDrive\College\Programs>g++-std=c++0xttd.cppttd.cpp:Infunction'intmain()':ttd.cpp:11:2

C++11 lambda 不明确

为什么编译器不能选择最明显的重载:#include#includestaticvoidfoo(conststd::function&f){std::cerr&f){std::cerr你会期望输出:usingbooloverloadtrueusingintoverload1但是,编译器无法推断出正确的重载:gcc-4.8:main.cpp:Infunction'intmain()':main.cpp:17:6:error:callofoverloaded'foo(main()::__lambda0)'isambiguous});^main.cpp:17:6:note:candidates

c++ - 将 boost::bimap 转换为 std::map

将bimap转换为std::map的明显方法似乎不起作用。下面是正确/好的转换方法吗?有更好/更短的方法吗?typedefboost::bimapMapType;MapType_bimap;//Fill_bimapMapType::left_map&lmap=_bimap.left;//std::mapbmap(lmap.begin(),lmap.end());//THISDOESNTWORKstd::mapbmap;BOOST_FOREACH(MapType::left_const_referenceentry,lmap){bmap[entry.first]=entry.second

c++ - std::map 是否要求比较器的 operator() 为常量?

在OSX10.8上使用libc++时,以下代码无法使用XCode4.5的clang++进行编译:#include#includeclassFoo{public:explicitFoo(intval_):val(val_){}intval;};structFooComparator{booloperator()(constFoo&left,constFoo&right){returnleft.valm;Foof(4);m[f]=std::string("four");return0;}错误:broken.cpp:11:8:note:candidatefunctionnotviable:'

c++ - 如何有效地使用 C++11 随机生成器?

我正在执行计算实验,这需要可重现。因此每个实验都使用自己的随机数生成器并记住其种子:classExperiment{public:voidoperator()();private:unsignedseed_;std::mt19937engine_;};问题是引擎需要传递给最基本的功能。假设在调用堆栈下方10层的某处有一个简单的函数,它需要一个引擎来生成一个介于0和1之间的随机数。然后该引擎需要传递给这10个调用中的每一个,使代码成为乱七八糟。我考虑并拒绝了这两种方法:1.globalengine:我会有一个全局引擎,所有基本函数都会调用这个引擎。但是,如果我想在不同的线程中运行多个实验